package ua.com.jpy.common;

import java.net.URL;

import org.slf4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import ua.com.jpy.entity.catagory.product.dao.ProductDao;

import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;

/**
 * @author LSD25
 *
 */
//This class using only for testing!!!
@Component("productDao")
public class StartServer {
	
	private static final String logFilename = "StarterServer";
	
	private static Logger log = LoggerFactory.getLogger(StartServer.class);
	
	@Value("${database.name}")
	private static String dbName;
	
	@Value("${mongo.user.name}")
	private static String username;
	
	@Value("${mongo.user.password}")
	private static String password;
	
	@Value("${is.authenticated}")
	private static boolean isAuthenticated;
	
	@Autowired
	protected StartServer(Mongo mongo, Morphia morphia) {
//		super(new DatastoreImpl(morphia, mongo, dbName));
		log.info(morphia + " " + mongo + " " + dbName);
//		if (isAuthenticated) {
//			if (!this.ds.getDB().isAuthenticated() && !this.ds.getDB().authenticate(username, password.toCharArray())) {
//				throw new RuntimeException("MongoDB authentication failed: " + dbName);
//			}
//		}
	}
	
	@SuppressWarnings({ "resource", "unused" })
	public static void main(String[] args) {
		System.setProperty("file.name", logFilename);
		URL url = StartServer.class.getClassLoader().getResource("log4j.xml");
		DOMConfigurator.configure(url);
		try {
			AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/app-context.xml");
			String dataBase = (String) context.getBean("dataBase");
			String host = (String) context.getBean("host");
			String port = (String) context.getBean("port");
			String mongoUserName = (String) context.getBean("mongoUserName");
			String mongoUserPassword = (String) context.getBean("mongoUserPassword");
			Boolean isAuthenticated = (Boolean) context.getBean("isAuthenticated");
			Mongo jpy = (Mongo) context.getBean("jpy");
			Morphia morphia = (Morphia) context.getBean("morphia");
			ProductDao productDao = (ProductDao) context.getBean("productDao");
			log.info(productDao.toString());
		} catch(Exception exc) {
			log.error("Failed load context!!!");
		}
	}

}
